﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using RCREEE.Library;
using RCREEE.Library.Entities;

namespace RCREEE.Web
{
    public partial class TestSitemap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RefereshSitemapNodes();
        }
        private void RefereshSitemapNodes()
        {
            List<CustomSiteMapNode> sdf = DataManager.GetSiteMapNodes(1);
            treesiteMap.Nodes.Clear();


            BindTree(treesiteMap, sdf, null);
        }

        private void BindTree(TreeView treeView, IEnumerable<CustomSiteMapNode> list, TreeNode parentNode)
        {
            var nodes = list.Where(x => parentNode == null ? x.ParentId == 0 : x.ParentId == int.Parse(parentNode.Value));
            foreach (var node in nodes)
            {
                //TreeNode newNode = new TreeNode(node.Url, node.SiteMapNodeId.ToString(), "", node.Url,"");
                TreeNode newNode = new TreeNode(node.Title.Name, node.SiteMapNodeId.ToString(),"",node.Url,"");
                if (parentNode == null)
                {
                    treeView.Nodes.Add(newNode);
                }
                else
                {
                    parentNode.ChildNodes.Add(newNode);
                }
                BindTree(treeView, list, newNode);
            }
        }
    }
}